/* 
    Purpose: Using the output from 1b_Census1960_mothers30to50.do,
    		 this file calculates average income at various levels of
    		 variation, separately for farmers and laborers. The ratio
    		 of farmer to laborer average income (by level of variation)
    		 is then calculated.

    Note: The 1960 5% Census is big enough that it is not 
    	  necessary to weight averages. Perwt confirms that 
    	  everyone is assigned the same weight.

    Creates: All output files have the prefix "FarmerRatios_1960_mothers_"
*/

clear
set more off
cd "$Mydirectory1/1_DataSources/CensusData/"

*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*

	use ./input/Census1960_5pct_mothers30to50_adjustments.dta, clear 
	sum perwt, d 

* Keep farmers and laborers
	gen farmer = occ1950==100 | occ1950==123
	gen laborer = occ1950==810 | occ1950==820 | occ1950==830 | occ1950==840 
	
	keep if farmer==1 | laborer==1
	
* Count # of farmers and # of laborers at each level of variation
	egen ncell_L_all=total(laborer)
	egen ncell_L_race=total(laborer), by(race)
	egen ncell_L_south=total(laborer), by(south_merge)
	egen ncell_L_race_south=total(laborer), by(race south_merge)

	egen ncell_F_all=total(farmer)
	egen ncell_F_race=total(farmer), by(race)
	egen ncell_F_south=total(farmer), by(south_merge)
	egen ncell_F_race_south=total(farmer), by(race south_merge) 
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*

************************************
*** AVERAGE INCOME: FARM LABORERS
************************************
	tempfile fulldata
	save `fulldata'

foreach inc in person hh {

	use `fulldata', clear

	local number_obs "20"
	
	if "`inc'"=="person" local income "inctot"
	if "`inc'"=="fam" local income "ftotinc"
	if "`inc'"=="hh" local income "hh_income" 
	
	local measure "mean"
	
* 0. Overall 
	egen laborers_income_all_aux = `measure'(`income') if laborer==1
	egen laborers_income_all = mean(laborers_income_all_aux)
	
* 1a. By race
	egen laborers_income_aux=`measure'(`income') if laborer==1, by(race)
	egen laborers_income=mean(laborers_income_aux), by(race)

	//Note: No cell is too small at this level
	tab ncell_L_race 
	tab ncell_F_race
	
* 1b. By south 
	egen laborers_income_south_aux=`measure'(`income') if laborer==1, by(south_merge)
	egen laborers_income_south=mean(laborers_income_south_aux), by(south_merge)

	//Note: No cell is too small at this level
	tab ncell_L_south 
	tab ncell_F_south
	
* 2. Race x south
	egen laborers_income_byr_bys_aux=`measure'(`income') if laborer==1, by(race south_merge)
	egen laborers_income_byr_bys=mean(laborers_income_byr_bys_aux), by(race south_merge)
	
	tab ncell_L_race_south
	tab ncell_F_race_south
	tab race south_merge if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race for laborers will 
	        be assigned to the cell. */	
	replace laborers_income_byr_bys = laborers_income if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')
	
    //Account for in-kind income of laborers 
        /*Note: See https://data.nber.org/data-appendix/w23395/Appendix_May_2020.pdf
        		(page 11) for more info */
	replace laborers_income_all=1.19*laborers_income_all
	replace laborers_income=1.19*laborers_income
	replace laborers_income_south=1.19*laborers_income_south
	replace laborers_income_byr_bys=1.19*laborers_income_byr_bys
	
	drop *_aux
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*
	
*******************************
*** AVERAGE INCOME: FARMERS
*******************************

* 0. Overall 
	egen farmers_income_all_aux = `measure'(`income') if farmer==1
	egen farmers_income_all = mean(farmers_income_all_aux)

* 1a. By race
	egen farmers_income_aux=`measure'(`income') if farmer==1, by(race)
	egen farmers_income=mean(farmers_income_aux), by(race)
	
	//Note: No cell is too small at this level	
	tab ncell_L_race 
	tab ncell_F_race
	
* 1b. By south
	egen farmers_income_south_aux=`measure'(`income') if farmer==1, by(south_merge)
	egen farmers_income_south=mean(farmers_income_south_aux), by(south_merge)

	//Note: No cell is too small at this level		
	tab ncell_L_south
	tab ncell_F_south	

* 2. Race x south
	egen farmers_income_byr_bys_aux=`measure'(`income') if farmer==1, by(race south_merge)
	egen farmers_income_byr_bys=mean(farmers_income_byr_bys_aux), by(race south_merge)
	
	tab ncell_L_race_south
	tab ncell_F_race_south
	tab race south_merge if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')

	/*Note: If a cell has too few observations (i.e., <20)
	        the average national income by race for farmers will 
	        be assigned to the cell. */	
	replace farmers_income_byr_bys = farmers_income if (ncell_L_race_south<`number_obs' | ncell_F_race_south<`number_obs')

* Account for in-kind income of farmers
    /*Note: See https://data.nber.org/data-appendix/w23395/Appendix_May_2020.pdf
        		(page 12) for more info */
	replace farmers_income_all=1.35*farmers_income_all 
	replace farmers_income=1.35*farmers_income 
	replace farmers_income_south=1.35*farmers_income_south 
	replace farmers_income_byr_bys=1.35*farmers_income_byr_bys

	drop *_aux
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*
	
************************************************
*** RATIOS OF FARMER TO LABORER AVG. INCOME
************************************************
	gen ratio_farm_`inc'_all = farmers_income_all / laborers_income_all
	gen ratio_farm_`inc' = farmers_income / laborers_income
	gen ratio_farm_south_`inc' = farmers_income_south / laborers_income_south
	gen ratio_farm_byr_bys_`inc' = farmers_income_byr_bys / laborers_income_byr_bys

	keep race edu region_merge south_merge ratio* serial pernum
	
	label var ratio_farm_`inc'_all "Ratio of farmer to farm laborer (`inc') average income, all"
	label var ratio_farm_`inc' "Ratio of farmer to farm laborer (`inc') average income, by race"
	label var ratio_farm_south_`inc' "Ratio of farmer to farm laborer (`inc') average income, by south"
	label var ratio_farm_byr_bys_`inc' "Ratio of farmer to farm laborer (`inc') average income, by race x south"
	
	tempfile income_`inc'
	save `income_`inc''
	
}

	use `income_person', clear
	merge 1:1 serial pernum using `income_hh'
	drop _merge
	
*------------------------------------------------------------------------------------*
*------------------------------------------------------------------------------------*
*********************
* SAVE (BY LEVEL)
*********************
	compress
	
* Overall
	preserve
	keep if _n==1
	gen number=1
	keep number ratio_farm_person_all ratio_farm_hh_all
	
	save "./output/FarmerRatios_1960_mothers_all.dta", replace
	restore

* By race
	preserve
	bysort race: keep if _n==1
	keep race ratio_farm_person ratio_farm_hh
	
	save "./output/FarmerRatios_1960_mothers_byrace.dta", replace
	restore 
	
* By south
	preserve
	bysort south_merge: keep if _n==1
	keep south_merge ratio_farm_south_hh ratio_farm_south_person
	
	save "./output/FarmerRatios_1960_mothers_bysouth.dta", replace
	restore 	

* Race x south
	preserve
	bysort race south_merge: keep if _n==1
	keep race south_merge ratio_farm_byr_bys_hh ratio_farm_byr_bys_person
	
	save "./output/FarmerRatios_1960_mothers_byrace_bysouth.dta", replace
	restore
